package com.restaurant.application.reservation;

import com.google.common.base.Strings;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.DateTypes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.commonunits.utils.SnowFlake;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.customer.Customer;
import com.restaurant.application.customer.support.CustomerData;
import com.restaurant.application.indexes.reservationlist.AssistReservationList;
import com.restaurant.application.indexes.reservationlist.mysql.ReservationListBean;
import com.restaurant.application.indexes.reservationlist.mysql.ReservationListCriteria;
import com.restaurant.application.indexes.reservationlist.mysql.ReservationListImpl;
import com.restaurant.application.minifacade.data.ReservationPageBO;
import com.restaurant.application.reservation.support.AbstractReservation;
import com.restaurant.application.reservation.support.ReservationData;
import com.restaurant.application.shop.Shop;
import com.restaurant.application.shop.support.ShopData;
import com.restaurant.application.shoplist.ShopList;
import com.restaurant.application.util.CommonUtils;
import com.restaurant.application.util.Constant;
import com.restaurant.application.util.DateTool;

import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
public class ReservationImpl extends AbstractReservation {
    @Override
    public View getFullView() {
        return newCommonTable(DataModes.EDIT);
    }

    @Override
    public View getViewLead(ViewSpec viewSpec) {
        return null;
    }

    @Override
    public String init(ReservationData data) {
        String msg = getStr(data);
        if (!Strings.isNullOrEmpty(msg)) {
            return msg;
        }

//        get uid and id by name
        UID shopUid = getSingleton(ShopList.class).findShopByName(data.getShopName());
        ShopData shopData = getUnit(Shop.class, shopUid).findData();
        data.withShopUid(shopUid);

        UID customerUid = getGate();
        CustomerData customerData = getUnit(Customer.class, customerUid).findData();

//        create number
        String numberStart = R + shopData.getId() + DateTool.getCMNumber();
        List<ReservationListBean> beans = getIndex(ReservationListImpl.class).search(new ReservationListCriteria().withNumberLike(numberStart), null, 100000);
        String number = CommonUtils.getReservationNumber(numberStart, beans.size());

//        向基元写入数据
        getData().withId(SnowFlake.findUniqueId()).withCreateTime(DateTimeUtils.getCurrentMillisecond()).withNumber(number)
                .withCustomerUid(customerUid).withCustomerName(customerData.getNickName()).withCustomerId(customerData.getNumber())
                .withPhone(customerData.getPhone()).withState(STATE_START);

        perfect(data);

        ReservationListBean bean = getSingleton(AssistReservationList.class).createBean(getId(), getData());
        if (Objects.nonNull(bean)) {
            getIndex(ReservationListImpl.class).insert(bean);
        }
        return Constant.SUCCESS;
    }

    private String getStr(ReservationData data) {
        if (Strings.isNullOrEmpty(data.getShopName())) {
            return "请选择门店";
        }
        if (Objects.isNull(data.getDate())) {
            return "请选择日期";
        }
        if (Strings.isNullOrEmpty(data.getTime())) {
            return "请选择时间";
        }
        if (Objects.isNull(data.getPersonAmount()) || data.getPersonAmount() <= 0) {
            return "请填写人数";
        }
        if (Strings.isNullOrEmpty(data.getServiceName())) {
            return "请选择服务";
        }
        return null;
    }

    @Override
    public String update(ReservationData data) {
        perfect(data);
        return Constant.SUCCESS;
    }

    @Override
    public String cancel() {
        perfect(new ReservationData().withState(STATE_CANCEL));
        return Constant.SUCCESS;
    }

    @Override
    public ReservationPageBO getReservationBean() {
        ReservationData data = getData();
        return new ReservationPageBO()
                .withOrderId(data.getNumber())
                .withCreateTime(data.getCreateTime())
                .withShopDate(CommonUtils.longToTimeString(data.getDate()))
                .withShopTime(data.getTime())
                .withStatus(data.getState())
                .withShopName(data.getShopName())
                .withCount(data.getPersonAmount())
                .withRemark(data.getRemarks())
                .withService(data.getServiceName());
    }

    @Override
    public ViewUpdate modify(ReservationData data, int[] seq) {
        perfect(data.withState(STATE_ACCEPT));
        return returnPushInContainer(getSingleton(ReservationListImpl.class.getName()).getId(), false);
    }

    @Override
    public ViewUpdate refuse(int[] seq) {
        perfect(new ReservationData().withState(STATE_REFUSE));
        return returnPushInContainer(getSingleton(ReservationListImpl.class.getName()).getId(), false);
    }

    @Override
    public ViewUpdate cancel(int[] seq) {
        return returnPopFromContainer(getId(), false);
    }

    private View newCommonTable(DataModes dataModes) {
        PageForm pageForm = newFormShell(dataModes).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), pageForm);
    }

    private FormShell newFormShell(DataModes dataModes) {
        ReservationData data = getData();
        FormShell shell = new FormShell("预约/预约订单/", 8, 1);
        if (Objects.nonNull(data.getShopUid())) {

        }

        boolean editable = false;
        if (DataModes.EDIT.equals(dataModes)) {
            editable = true;
        }
        shell.source(data, dataModes);

        shell.at(0, 0, "预约门店")
                .getEntry(ReservationData.FN_SHOPNAME, data.getShopName())
                .placeHolder("预约门店")
                .selectFrom(CommonUtils.getAllShopName(this))
                .getContent().setEditable(editable);
        shell.at(1, 0, "预约日期")
                .getEntry(ReservationData.FN_DATE, DateTimeUtils.toTime(data.getDate()))
                .placeHolder("预约日期")
                .date(DateTypes.DATETIME, "yyyy-MM-dd")
                .getContent().setEditable(editable);
        shell.at(2, 0, "预约时间")
                .getEntry(ReservationData.FN_TIME, data.getTime())
                .placeHolder("预约时间")
                .selectFrom("08:00-10:30", "10:30-12:00", "17:00-20:00")
                .getContent().setEditable(editable);
        shell.at(3, 0, "预约人数")
                .getEntry(ReservationData.FN_PERSONAMOUNT, String.valueOf(data.getPersonAmount()))
                .placeHolder("预约人数")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(editable);
        shell.at(4, 0, "姓名")
                .getEntry(ReservationData.FN_CUSTOMERNAME, data.getCustomerName())
                .placeHolder("姓名")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(false);
        shell.at(5, 0, "手机号")
                .getEntry(ReservationData.FN_PHONE, data.getPhone())
                .placeHolder("手机号")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(false);
        shell.at(6, 0, "预约服务")
                .getEntry(ReservationData.FN_SERVICENAME, data.getServiceName())
                .placeHolder("预约服务")
                .selectFrom("早餐", "下午茶", "午餐")
                .getContent().setEditable(editable);
        shell.at(7, 0, "备注")
                .getEntry(ReservationData.FN_REMARKS, data.getRemarks())
                .placeHolder("备注")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(editable);
        if (editable) {
            shell.at(8, 0, "修改并确认预约").buttonField(getPath(CMD_MODIFY), "", true);
            shell.at(8, 1, "拒绝").buttonField(getPath(CMD_REFUSE), "", true);
        }
        return shell;
    }

    private void perfect(ReservationData data) {
        getData().withModifyTime(DateTimeUtils.getCurrentMillisecond());
        if (!Strings.isNullOrEmpty(data.getShopName())) {
            getData().withShopName(data.getShopName()).withShopUid(getSingleton(ShopList.class).findShopByName(data.getShopName()));
        }
        if (!Strings.isNullOrEmpty(data.getServiceName())) {
            getData().withServiceName(data.getServiceName());
        }
        if (Objects.nonNull(data.getPersonAmount()) && data.getPersonAmount() > 0) {
            getData().withPersonAmount(data.getPersonAmount());
        }
        if (Objects.nonNull(data.getDate())) {
            getData().withDate(data.getDate());
        }
        if (!Strings.isNullOrEmpty(data.getTime())) {
            getData().withTime(data.getTime());
        }
        if (!Strings.isNullOrEmpty(data.getPhone())) {
            getData().withPhone(data.getPhone());
        }
        if (!Strings.isNullOrEmpty(data.getState())) {
            getData().withState(data.getState());
        }
        if (!Strings.isNullOrEmpty(data.getRemarks())) {
            getData().withRemarks(data.getRemarks());
        }
    }

    @Override
    public ReservationListBean getReservationUidDataForReservationList(ReservationListBean bean) {
        return getSingleton(AssistReservationList.class).updateBean(getId(), getData(), bean);
    }
}
